

#import "FAMHomeViewController.h"


@interface FAMHomeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *FAMTableView;
@property (nonatomic, strong)FAMFoodrecordListModel *FAMHomeListModel;
@property (nonatomic, strong)FAMHomeTableViewHeaderView * FAMTableHeaderView;
@end

@implementation FAMHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self FAM_showUi];
    [FAMUserManger shareInstance].FAMTimer = [FAMTool FAM_getYearWithMonthWithDay];
    [self FAM_loadData:[FAMUserManger shareInstance].FAMTimer isFirst:YES];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FAM_notificationAction:) name:@"FAMHomeLoadData" object:nil];
    
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"FAMHomeLoadData" object:nil];
}
- (void)FAM_showUi{
    self.fd_prefersNavigationBarHidden = YES;
    self.FAMTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [self.FAMTableView registerNib:[UINib nibWithNibName:@"FAMHomeTableViewtableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.FAMTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    FAMHomeTableViewHeaderView * tableHeaderView = [[FAMHomeTableViewHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 568)];
    self.FAMTableHeaderView = tableHeaderView;
    FAMHomeTableViewFooterView *tableFooterView = [[FAMHomeTableViewFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 145)];
    //568
    UIView *tempHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 568)];
    [tempHeaderView addSubview:tableHeaderView];
    UIView *tempFooterView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250)];
    [tempFooterView addSubview:tableFooterView];
    self.FAMTableView.tableHeaderView = tempHeaderView;
    self.FAMTableView.tableFooterView = tempFooterView;
    WeakSelf(self);
    self.FAMTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf FAM_loadData:[FAMUserManger shareInstance].FAMTimer isFirst:false];
    }];
}
- (void)FAM_notificationAction:(NSNotification *)sender{
    [self FAM_loadData:[FAMUserManger shareInstance].FAMTimer isFirst:YES];
}
- (void)FAM_loadData:(NSString *)date isFirst:(BOOL)isFirst{
    WeakSelf(self);
    if (isFirst) {
        [self showHudInKeyWindowWithHint:nil];
    }
    [FAMHttpTool FAM_GetFoodmateFoodrecordWithDate:date Success:^(FAMFoodrecordListModel * _Nonnull model, NSString * _Nonnull msg) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [FAMHttpTool FAM_GetFoodmateUserdata:^(FAMGetFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull msg) {
                [FAMUserManger shareInstance].userModel = model;
                dispatch_async(dispatch_get_main_queue(), ^{
                    weakSelf.FAMTableHeaderView.FAMFoodmateUserdataModel = model;
                });
            } failure:^(NSString * _Nonnull errorInfo) {

            }];
            if (isFirst) {
                [weakSelf hideHudKeyWindow];
            }else{
                [weakSelf.FAMTableView.mj_header endRefreshing];
            }
            weakSelf.FAMHomeListModel = model;
            weakSelf.FAMTableHeaderView.FAMFoodListModel = model;
            [weakSelf.FAMTableView reloadData];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (isFirst) {
                [weakSelf hideHudKeyWindow];
            }else{
                [weakSelf.FAMTableView.mj_header endRefreshing];
            }
            [weakSelf showHint:errorInfo];
        });
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FAMHomeTableViewtableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.row = indexPath.row;
    cell.FAMHomeListModel = self.FAMHomeListModel;
    cell.FAMDate = [FAMUserManger shareInstance].FAMTimer;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 120 + self.FAMHomeListModel.breakfast.count*80;
    }else if (indexPath.row == 1){
        return 120 + self.FAMHomeListModel.lunch.count*80;
    }else if (indexPath.row == 2){
        return 120 + self.FAMHomeListModel.dinner.count*80;
    }else{
        return 120 + self.FAMHomeListModel.extra.count*80;
    }
}

@end
